import * as mobx from 'mobx';
import ajax from 'common/utils/ajax';
import Base from 'store/model/base';
const { observable, action, runInAction, computed } = mobx;

/**
 * 商品列表
 */
export default class Item extends Base {
  @observable state = 'done'; // done, pending
  @observable itemId = 0;
  @observable title = '';
  @observable type = 'realGoods';
  @observable number = 1;
  @observable img = '';
  @observable probability = 1;
  @observable content = '';

  @computed get loading() {
    return this.state === 'pending';
  }

  @action async getItemDetail(itemId) {
    this.state = 'pending';
    try {
      this.itemId = itemId;
      const data = await ajax.req('item.getItemDetail', {
        itemId,
      },);
      runInAction(() => {
        this.state = 'done';

        this.title = data.title;
        this.type = data.type;
        this.number = data.number;
        this.img = data.img;
        this.probability = data.probability;
        this.content = data.content;
      });
    } catch (e) {
      runInAction(() => {
        this.state = 'error';
      });
    }
  }

  @action async addItem(params) {
    this.state = 'pending';
    try {
      const data = await ajax.req('item.addItem', {
        itemId: this.itemId,
        ...params
      }, 'post');
      if (data.res) {
        runInAction(() => {
          this.state = 'done';

          // this.itemId = params.id;
          this.title = params.title;
          this.type = data.type;
          this.number = params.number;
          this.img = params.img;
          this.probability = params.probability;
          this.content = params.content;
        });
      }
    } catch (e) {
      runInAction(() => {
        this.state = 'error';
      });
    }

  }



}

